package com.iceframework.core.utils;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iceframework.core.entity.User;
import com.iceframework.core.support.security.entity.IJWTInfo;
import com.iceframework.core.support.security.util.JwtUtils;


public class WebUtils {

	public static String TOKEN_NAME = "token";

	/**
	 * 获取token
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static String getToken(HttpServletRequest request) throws Exception {
		String token = WebUtils.getCookie(request, TOKEN_NAME);
		if (StringUtils.isBlank(token)) {
			token = request.getHeader(TOKEN_NAME);
			if (StringUtils.isBlank(token)) {
				token = request.getParameter(TOKEN_NAME);
				if (StringUtils.isBlank(token)) {
					token = (String) request.getAttribute(TOKEN_NAME);
					if (StringUtils.isBlank(token)) {
						token = request.getParameter(TOKEN_NAME);
					}
				}
			}
		}
		return token;
	}

	/**
	 * 获取token
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static String getToken() throws Exception {
		HttpServletRequest request = getRequest();
		return getToken(request);
	}

	/**
	 * 得到request
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 得到session
	 * 
	 * @return
	 */
	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 获取登录用户信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public static IJWTInfo getUser() {
		try {
			String token = getToken();
			IJWTInfo jwtInfo = JwtUtils.getJWTInfoByToken(token);
			return jwtInfo;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 得到用户ID
	 * 
	 * @return
	 */
	public static String getUserId() {
		IJWTInfo user = getUser();
		if (user != null) {
			return user.getUserId();
		}
		return "";
	}

	/**
	 * 得到用户编号和用户ID一样
	 * 
	 * @return
	 */
	public static String getUserNo() {
		return getUserId();
	}

	/**
	 * 得到用户名称
	 * 
	 * @return
	 */
	public static String getUserName() {
		IJWTInfo user = getUser();
		String userInfoJson = user.getUserInfo();
		User userInfo = JsonUtils.convertString2Obj(userInfoJson, User.class);
		if (userInfo != null) {
			return userInfo.getUsername();
		}
		return "";
	}

	/**
	 * 所有的参数
	 * 
	 * @return
	 */
	public static Map<String, String[]> getParameters() {
		Map<String, String[]> mapps = getRequest().getParameterMap();
		return mapps;
	}

	/**
	 * 获取cookie值
	 * 
	 * @param request    http请求
	 * @param cookieName cookie名称
	 * @return
	 */
	public static String getCookie(HttpServletRequest request, String cookieName) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookieName.equals(cookie.getName())) {
					String value = cookie.getValue();
					return value == null ? "" : value;
				}
			}
		}
		return "";
	}

	/**
	 * 从指定cookie数组中获取cookieName对应的cookie信息
	 * 
	 * @param cookies    cookie数组
	 * @param cookieName cookie名称
	 * @return
	 */
	public static Cookie getCookie(Cookie[] cookies, String cookieName) {
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookieName.equals(cookie.getName())) {
					return cookie;
				}
			}
		}
		return null;
	}
	
	/**
	 * 以JSON格式输出
	 * 
	 * @param response
	 * @throws IOException
	 */
	public static void responseOutWithJson(HttpServletResponse response, Object responseObject) throws IOException {
		// 将实体对象转换为JSON Object转换
		String convertObj2String = JsonUtils.convertObj2String(responseObject);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			out.print(convertObj2String);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}


}